package cn.itsource.pet.controller;

import cn.itsource.basic.util.LoginContext;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.pet.dto.AcceptDto;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.user.domain.Logininfo;
import cn.itsource.user.domain.User;
import cn.itsource.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null) {
                searchMasterMsgService.update(searchMasterMsg);
            } else {
                searchMasterMsgService.add(searchMasterMsg);
            }
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryByPage(query);
    }

    /**
     * publish发布寻主接口
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request)
    {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        User user = userService.findByLogininfoId(logininfo.getId());
        searchMasterMsg.setUserId(user.getId());
        return searchMasterMsgService.publish(searchMasterMsg);
    }

    /**
     * 查询店铺待处理数据
     * state=1&shopId=当前登录人shopId
     * @param searchMasterMsgQuery
     * @param request
     * @return
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request)
    {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.selectByLogininfoId(logininfo.getId());
        searchMasterMsgQuery.setShopId(employee.getShopId());
        searchMasterMsgQuery.setState(1);
        return searchMasterMsgService.selectSearchMasterMessageByParam(searchMasterMsgQuery);
    }

    /**
     * 查询待发布数据
     * state=0
     * @param searchMasterMsgQuery
     * @return
     */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery)
    {
        searchMasterMsgQuery.setState(0);
        return searchMasterMsgService.selectSearchMasterMessageByParam(searchMasterMsgQuery);
    }

    /**
     * 查询寻主池数据
     * state=1AndShop=null
     * @param searchMasterMsgQuery
     * @return
     */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery)
    {
        searchMasterMsgQuery.setState(1);
        return searchMasterMsgService.selectSearchMasterMessageByParam(searchMasterMsgQuery);
    }

    /**
     * 查询已完成寻主消息数据
     * state=3AndShop=null
     * @param searchMasterMsgQuery
     * @param request
     * @return
     */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request)
    {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.selectByLogininfoId(logininfo.getId());
        // 等于null，那么就是平台管理员，那么shopId我们就是要null，那么就不需要管

        // 如果不等于null，那么久需要设置shopId
        if (employee.getShopId() != null){
            searchMasterMsgQuery.setShopId(employee.getShopId());
        }

        searchMasterMsgQuery.setState(3);
        return searchMasterMsgService.selectSearchMasterMessageByParam(searchMasterMsgQuery);
    }


    /**
     * 给用户个人中心提供的结构，state传递什么，那么就查询什么
     * @param searchMasterMsgQuery
     * @return
     */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery)
    {
        return searchMasterMsgService.selectSearchMasterMessageByParam(searchMasterMsgQuery);
    }

    /**
     * 根据
     * @param acceptDto
     * @return
     */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptDto acceptDto)
    {
        searchMasterMsgService.accept(acceptDto);
        return AjaxResult.me().success();
    }


    /**
     * 根据msgId进行拒单
     * @param msgId
     * @param request
     * @return
     */
    @PostMapping("/reject/{msgId}")
    public AjaxResult reject(@PathVariable("msgId") Long msgId, HttpServletRequest request)
    {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.selectByLogininfoId(logininfo.getId());
        searchMasterMsgService.reject(msgId, employee);
        return AjaxResult.me().success();
    }

}
